package com.kerrykidz.workflow.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kerrykidz.common.Constants;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.dao.model.HrmLeaveEntity;
import com.kerrykidz.workflow.service.ILeaveWorkflowService;
import com.kerrykidz.workflow.vo.TypeVo;
import com.kerrykidz.workflow.vo.LeaveVo;
import com.kerrykidz.workflow.vo.SearchParamVo;
import com.kerrykidz.workflow.vo.WorkflowParamVo;
import com.rdp.framework.exception.RDPException;

/**
 * 请假流程控制器
 * @author zhuss
 *
 */
@Controller
@RequestMapping(value = "/workflow/leave")
public class LeaveController {
	private static Logger logger = LoggerFactory.getLogger(LeaveController.class);
	 
	 @Autowired
	 ILeaveWorkflowService leaveWorkflowService;
	 
	 /**
	  * 请假类型
	  * @return
	  */
	 @RequestMapping(value = "findLeaveTypeList")
	 @ResponseBody
	 public List<TypeVo> findLeaveTypeList() throws RDPException{
		 return leaveWorkflowService.findLeaveTypeList();
	 }
	 
	 @RequestMapping(value="/findLeaveList",method=RequestMethod.GET)
	 public String findLeaveListGet(HttpSession session){
		UserInfo user = UserUtil.getUserFromSession();
		if (null != user) {
			if (user.getRoleList().contains("102")
					|| user.getRoleList().contains("101")) {
				session.setAttribute("isPass", "1");
			} else
				session.removeAttribute("isPass");;

		}
		return "hr/hr_leave_list";
	 }
	 
	 /**
	  * 查询请假列表
	  * @return
	  * @throws RDPException
	  */
	 @RequestMapping(value="/findLeaveList",method=RequestMethod.POST)
	 @ResponseBody
	public SearchParamVo findLeaveListPost(SearchParamVo searchParam,HttpSession session) throws RDPException {
		 logger.debug("SearchParamVo request: ",searchParam);
		 UserInfo user = UserUtil.getUserFromSession();
		if(null != searchParam){
			if(null != user ){
				searchParam.setUserInfo(user);
				List<LeaveVo> volist = leaveWorkflowService.findLeaveList(searchParam);
				searchParam.setResults(volist);
				if(user.getRoleList().contains("102")||user.getRoleList().contains("101")){
					session.setAttribute("isPass", "1");
				}else
					session.removeAttribute("isPass");;
				
			}
		}
		return searchParam;
	}

	/**
	 * 创建请假
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/createLeave")
	public ModelAndView createLeave(HttpSession session) {
		ModelAndView mv = new ModelAndView("hr/hr_leave_new");
		UserInfo user = UserUtil.getUserFromSession();
		if(null != user){
			mv.addObject("applyName", user.getName());
		}
		logger.debug("response : applyname={}",UserUtil.getUserFromSession().getName());
		return mv;
	}

	/**
	 * 编辑详情
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/detailLeave")
	@ResponseBody
	public ModelAndView detailLeave(@RequestParam("id") String id,
			HttpSession session) throws RDPException, UnsupportedEncodingException {
		ModelAndView mv = new ModelAndView("hr/hr_leave_detail");
		UserInfo user = UserUtil.getUserFromSession();
		if(null != user){
			LeaveVo vo = leaveWorkflowService.findLeaveById(id,user);
			mv.addObject("detailLeave", vo);
		}
		return mv;
	}

	 
	 /**
	  * 保存请假
	  * @return
	  */
	 @RequestMapping("/saveLeave")
	 public String saveLeave(HrmLeaveEntity entity,@RequestParam("btnType") String btnType,HttpSession session, RedirectAttributes redirectAttributes)  throws RDPException{
		 logger.debug("rquest : applyname={},btnType={}",entity.getApplyBy(),btnType);
		 UserInfo info = UserUtil.getUserFromSession();
		 if(null != info){
			 if(!info.getRoleList().contains("102")){
				 	leaveWorkflowService.saveLeave(entity,btnType,info);
				}else{
					 redirectAttributes.addFlashAttribute("message", "该登录角色不需要走请假流程!");
				}
		 }
		 return "redirect:/workflow/leave/findLeaveList.shtml";
	 }
	 
	 /**
	  * 修改请假
	  * @return
	  */
	 @RequestMapping("/updateLeave")
	 public String updateLeave(HrmLeaveEntity entity,@RequestParam("btnType") String btnType, HttpSession session, RedirectAttributes redirectAttributes)  throws RDPException{
		UserInfo user = UserUtil.getUserFromSession();
		if(null != user){
			if(!user.getRoleList().contains("102")||btnType.equals("4")){
				leaveWorkflowService.updateLeave(entity,btnType,user);
			}else{
				 redirectAttributes.addFlashAttribute("message", "该登录角色不需要走请假流程!");
			}
		}
		return "redirect:/workflow/leave/findLeaveList.shtml";
	 }
	 
	
	/**
	 * 完成任务
	 * @param taskId
	 * @param var
	 * @return
	 */
	@RequestMapping(value = "/complete", method = RequestMethod.POST)
	public String complete(WorkflowParamVo workflowparam,HttpSession session) throws RDPException{
		UserInfo userInfo = UserUtil.getUserFromSession();
		if(null != workflowparam){
			if(null != userInfo && StringUtils.isNotBlank(workflowparam.getBusinessKey())){
				workflowparam.setUserInfo(UserUtil.getUserFromSession());
				leaveWorkflowService.completeLeave(workflowparam);
				return "redirect:/workflow/leave/findLeaveList.shtml";
			}
		}
		return Constants.FAILURE;
    }
}
